import {combineReducers} from 'redux-immutable';
import * as actions from './actions';
import Immutable from 'immutable';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION} from './constants';
import {formatMessage} from './intl';
const initData = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
            });
        case actions.GET_INIT_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                data: action.data.content
            });
        case actions.GET_INIT_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};

const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                hasError: '',
                data: action.data
            });
        case actions.GET_LIST_DATA_FAIL:
            return state.merge({
                isFetching: false,
                data: {},
                hasError: action.message,
            });
        default:
            return state;
    }
};

const listType = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_TYPE_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_TYPE_SUCCESS:
            return state.merge({
                isFetching: false,
                hasError: '',
                data: [
                    ...action.data,
                    {
                        id: '00000-000000-00000-000000-00000',
                        month: formatMessage({
                            id: 'tablePanel.column.total',
                            defaultMessage: '合计：'
                        }),
                    }
                ]
            });
        case actions.GET_LIST_TYPE_FAIL:
            return state.merge({
                isFetching: false,
                data: [],
                hasError: action.message,
            });
        default:
            return state;
    }
};


const listCountry = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_COUNTRY_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_COUNTRY_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                hasError: '',
                data: action.data.length > 0 ? action.data.map(item => ({
                    country: item.country,
                    id: item.id,
                    details: [
                        ...item.details,
                        {
                            id: '00000-000000-00000-000000-00000',
                            month: formatMessage({
                                id: 'tablePanel.column.total',
                                defaultMessage: '合计：'
                            }),
                        }]
                })) : []
            });
        case actions.GET_LIST_COUNTRY_DATA_FAIL:
            return state.merge({
                isFetching: false,
                data: [],
                hasError: action.message,
            });
        default:
            return state;
    }
};

export const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(Immutable.fromJS({
                isFetching: true,
                message: ''
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: '',
                data: action.data
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: action.message
            }));
        default:
            return state;
    }
};
const appState = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_CONDITION:
            return state.setIn(['queryCondition', action.name], action.value);
        case actions.RESET_QUERY_PANEL:
            return state.set('queryCondition', Immutable.fromJS(DEFAULT_QUERY_OPTION));
        case actions.RESET_QUERY_PANEL_DEALER:
            return state.setIn(['queryCondition', 'dealerId'], []);
        default:
            return state;
    }
};
const domainData = combineReducers({
    initData,
    list,
    listType,
    listCountry,
    permission,
});

export default combineReducers({
    domainData,
    appState,
});
